<?php
//自动生成,请根据需要修改
namespace app\redenvelope\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;
/**
* @title 收到红包列表接口
* @description 收到红包列表说明
* @group 红包
*/
class RedReceive extends Base{

	/**
	* @title 插入记录
	* @description 插入收到红包列表数据
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/add
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:receive_id type:Int require:0 default:0 other: desc:接收者
	* @param name:amount type:Decimal require:0 default:0 other: desc:抢到红包金额
	* @param name:message type:VarChar require:0 default:0 other: desc:留言
	* @param name:receive_time type:Int require:0 default:0 other: desc:抢到红包时间
	*
	*
	* @return receive_id:接收者
	* @return amount:抢到红包金额
	* @return message:留言
	* @return receive_time:抢到红包时间
	*
	*/
	public function add (){
		//模块 - 输入数据
		$input['receive_id'] = Request::post('receive_id/d');
		$input['amount'] = Request::post('amount');
		$input['message'] = Request::post('message');
		$input['receive_time'] = Request::post('receive_time/d');

		//模块 - 验证器
		$red_receiveValidate = new \app\redenvelope\validate\RedReceive;
		$validate = $red_receiveValidate->scene('except_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $red_receiveValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 新增  ---
		//插入的数据
		$insert_data = [
			'receive_id'=>$input['receive_id'],
			'amount'=>$input['amount'],
			'message'=>$input['message'],
			'receive_time'=>$input['receive_time'],
		];
		//添加数据
		$insert_res = Db::name('red_receive')->data($insert_data)->insert();
		if(!$insert_res){
			$msg['msg'] = '插入数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $insert_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（paginate）
	* @description 获取收到红包列表的列表数据（分页）
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/get_list
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:receive_id type:Int require:0 default:0 other: desc:接收者
	* @param name:amount type:Decimal require:0 default:0 other: desc:抢到红包金额
	* @param name:message type:VarChar require:0 default:0 other: desc:留言
	* @param name:receive_time type:Int require:0 default:0 other: desc:抢到红包时间
	*
	* @param name:page type:Int require:0 default:0 other: desc:当前页
	* @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
	*
	* @return id:id
	* @return receive_id:接收者
	* @return amount:抢到红包金额
	* @return message:留言
	* @return receive_time:抢到红包时间
	*
	*/
	public function get_list (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');
		$input['receive_id'] = Request::param('receive_id/d');
		$input['amount'] = Request::param('amount');
		$input['message'] = Request::param('message');
		$input['receive_time'] = Request::param('receive_time/d');


		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['receive_id','=',$input['receive_id']],
			['amount','=',$input['amount']],
			['message','like','%'.$input['message'].'%'],
			['receive_time','=',$input['receive_time']],
		];
		//去除为空的条件
		$paginate_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($paginate_map,$v);
		}

		//获取数据
		$select_res = Db::name('red_receive')->where($paginate_map)->paginate();

		// paginate 返回数据
		$msg['list'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取列表数据（select）
	* @description 获取收到红包列表的列表数据（无分页）
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/get_select
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:0 default:0 other: desc:id
	* @param name:receive_id type:VarChar require:1 default:0 other: desc:接收者
	* @param name:amount type:Decimal require:0 default:0 other: desc:抢到红包金额
	* @param name:message type:VarChar require:0 default:0 other: desc:留言
	* @param name:receive_time type:Int require:0 default:0 other: desc:抢到红包时间
	*
	*
	* @return id:id
	* @return receive_id:接收者
	* @return amount:抢到红包金额
	* @return message:留言
	* @return receive_time:抢到红包时间
	* @return logo:头像
	*
	*/
	public function get_select (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');
		$input['receive_id'] = Request::param('receive_id');
		$input['amount'] = Request::param('amount');
		$input['message'] = Request::param('message');
		$input['receive_time'] = Request::param('receive_time/d');


		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
			['amount','=',$input['amount']],
			['message','like','%'.$input['message'].'%'],
			['receive_id','like','%'.$input['receive_id'].'%'],
			['receive_time','=',$input['receive_time']],
		];
		//去除为空的条件
		$select_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($select_map,$v);
		}

		//获取数据
		$select_res = Db::name('red_receive')->where($select_map)->select();


		//获取收到红包的数量
        $receive_num=Db::name('red_receive')->where('receive_id',$input['receive_id'])->count();

		// select 返回数据
		$msg['list'] = $select_res;

		$msg['num']=$receive_num;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 获取单条数据
	* @description 根据ID获取收到红包列表的指定记录
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');

		// input结果返回
		$msg['data']['id'] = $input['id'];

		//模块 - 验证器
		$red_receiveValidate = new \app\redenvelope\validate\RedReceive;
		$validate = $red_receiveValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $red_receiveValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('red_receive')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新收到红包列表指定记录的数据
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:receive_id type:Int require:0 default:0 other: desc:接收者
	* @param name:amount type:Decimal require:0 default:0 other: desc:抢到红包金额
	* @param name:message type:VarChar require:0 default:0 other: desc:留言
	* @param name:receive_time type:Int require:0 default:0 other: desc:抢到红包时间
	*
	*
	* @return id:id
	* @return receive_id:接收者
	* @return amount:抢到红包金额
	* @return message:留言
	* @return receive_time:抢到红包时间
	*
	*/
	public function edit (){
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['receive_id'] = Request::post('receive_id/d');
		$input['amount'] = Request::post('amount');
		$input['message'] = Request::post('message');
		$input['receive_time'] = Request::post('receive_time/d');


		//模块 - 验证器
		$red_receiveValidate = new \app\redenvelope\validate\RedReceive;
		$validate = $red_receiveValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $red_receiveValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'receive_id'=>$input['receive_id'],
			'amount'=>$input['amount'],
			'message'=>$input['message'],
			'receive_time'=>$input['receive_time'],
		];
		//获取数据
		$update_res = Db::name('red_receive')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;

		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 按条件删除记录
	* @description 按条件删除收到红包列表的数据
	* @author 开发者（范浩婷）
	* @url /redenvelope/red_receive/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:receive_id type:Int require:0 default:0 other: desc:接收者
	* @param name:amount type:Decimal require:0 default:0 other: desc:抢到红包金额
	* @param name:message type:VarChar require:0 default:0 other: desc:留言
	* @param name:receive_time type:Int require:0 default:0 other: desc:抢到红包时间
	*
	*
	* @return id:id
	* @return receive_id:接收者
	* @return amount:抢到红包金额
	* @return message:留言
	* @return receive_time:抢到红包时间
	*
	*/
	public function delete (){
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['receive_id'] = Request::post('receive_id/d');
		$input['amount'] = Request::post('amount');
		$input['message'] = Request::post('message');
		$input['receive_time'] = Request::post('receive_time/d');


		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['id','=',$input['id']],
			['receive_id','=',$input['receive_id']],
			['amount','=',$input['amount']],
			['message','=',$input['message']],
			['receive_time','=',$input['receive_time']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('red_receive')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}

}
